package com.qf.controller;

import com.qf.pojo.*;
import com.qf.service.AddressService;
import com.qf.service.OrderService;
import com.qf.service.impl.AddressServiceImpl;
import com.qf.service.impl.OrderServicImpl;
import com.qf.utils.SYS;
import com.qf.vo.MsgResult;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.sql.ResultSet;
import java.util.Collection;
import java.util.Date;
import java.util.UUID;


public class OrderController extends Base{

    private AddressService addressService=new AddressServiceImpl();
    private OrderService orderService=new OrderServicImpl();


    //生成一个订单
    public String add(HttpServletRequest req, HttpServletResponse resp){
        //接受页面来的大量参数
        HttpSession session=req.getSession();
        ShopCart shopCart= (ShopCart) session.getAttribute(SYS.CART);
        User user= (User) session.getAttribute(SYS.USER);

        String[] ids=req.getParameterValues("gid");
        MsgResult msg= shopCart.show(ids);
        Collection<ShopItem> shopItems=msg.getItemList();

        String addrid=req.getParameter("useraddress");
        Integer aid=Integer.valueOf(addrid);
        Address address=addressService.selectOne(aid);
        //生成一个订单
        /*
       private  String oid;//自行生成
        private  Integer uid;
        private Date createtime;//系统当前时间
        private Float total;    //后端计算
        private String uname;
        private String utel;
        private String addr;
        private Integer status;//默认先0
        */
        String oid= UUID.randomUUID().toString().replaceAll("-","");//生成一个随机订单编号
        Integer uid=user.getId();
        Date creattime=new Date();
        Float total=msg.getTotal();
        String uname=address.getUname();
        String utel=address.getUtel();
        String addr=address.getContent();

        //public Order(String oid, Integer uid, Date createtime, Float total, String uname, String utel, String addr, Integer status)
        Order order=new Order(oid,uid,creattime,total,uname,utel,addr,0);

        int res=orderService.addOrder(order,shopItems);
        if(res==1){
            shopCart.deleteOne(ids);
            req.setAttribute("order",order);
            return "z:success.jsp";
        }
      return "z:res.jsp";
    }
}
